<template>
  <FormView
    v-model="entity"
    :title="entity.id ? '委托信息' : '新建委托'"
    :toolbar-attrs="{
      buttons: [
        'back',
        {
          save: isDraft || flowContext.isStartNode,
          submit: hasBtn('submit'),
          withdraw: flowContext.canWithdraw,
          reject: flowContext.canReject,
          finish: hasBtn('finish')
        }
      ]
    }"
    :toolbar-listeners="{
      save: () => saveAndStart({ title: `${userName}的项目委托申请` }),
      submit: submitAudit,
      finish: () => submitAudit({}),
      withdraw: () => withdrawAudit(),
      reject: () => reject()
    }">
    <template #toolbar-right> </template>
    <FormHolder ref="form" label-width="130px" :disabled="isDisabled" :model="entity">
      <FormItemGroup block-title="基本信息">
        <FormInput v-model="entity.entrustedBusiness" prop="entrustedBusiness" label="委托业务名称" required />
        <FormInputDepartment
          :id.sync="entity.departmentId"
          :name.sync="entity.departmentName"
          prop="departmentName"
          label="所属部门名称"
          @input="setDepartment" />
        <FormInput v-model="entity.major" prop="major" label="所属专业" required />
        <FormInput v-model="entity.contractNumber" prop="contractNumber" label="合同编号" />
        <FormInput v-model="entity.foundSource" prop="foundSource" label="经费来源" required />
        <FormInput v-model="entity.budget" prop="budget" label="预算金额" required />
        <FormDatePicker v-model="entity.auditTime" prop="auditTime" label="审查时间" required />
        <FormInput
          v-model="entity.projectName"
          prop="projectName"
          label="所属项目名称"
          readonly
          required
          :readonly-grey="false"
          suffix-icon="el-icon-search"
          @click.native="isProjectsDialogVisible = true" />
        <FormSelect v-model="entity.projectStatus" prop="projectStatus" label="项目状态" dict-type="project_status" required />
      </FormItemGroup>
      <FormItemGroup block-title="附件">
        <FormAttachments required prop="unid" label="附件" model="ProjectDelegate" hide-label :unid="entity.unid" :categories="attachmentTypes" />
      </FormItemGroup>
    </FormHolder>
    <ProjectsDialog :visible.sync="isProjectsDialogVisible" @confirm="setProject" />
  </FormView>
</template>

<script>
  import { save, create, load, update } from './api'
  import flowForm from '@/vue/mixin/flow-form'
  import FormAttachments from '@/components/form/FormAttachments.vue'
  import ProjectsDialog from '@/module/tech-project/project-manage/information/ProjectsDialog.vue'

  export default {
    name: 'ProjectDelegateForm',
    components: { ProjectsDialog, FormAttachments },
    docType: 'WF_PROJECT_DELEGATE',
    relativeViewId: 'ProjectDelegateView',
    mixins: [flowForm],
    data() {
      return {
        isProjectsDialogVisible: false,
        attachmentTypes: [
          {
            label: '经费来源证明文件',
            value: 'foundsSource',
            required: true
          },
          {
            label: '所属部门采购讨论会议记录',
            value: 'purchaseMeeting'
          },
          {
            label: '采购需求书',
            value: 'purchaseDemand'
          },
          {
            label: '调研报告',
            value: 'researchReport'
          },
          {
            label: '竞争性比选文件',
            value: 'compareFile'
          },
          {
            label: '采购文件',
            value: 'purchaseFile'
          },
          {
            label: '合同',
            value: 'contract'
          },
          {
            label: '其他',
            value: 'other'
          }
        ],
        entity: {
          id: null,
          unid: null,
          createTime: null,
          createBy: null,
          createId: null,
          deptId: null,
          deptFullName: null,
          updateTime: null,
          updateId: null,
          updateBy: null,
          tenantId: null,
          delFlag: null,
          entrustedBusiness: null,
          departmentId: null,
          departmentName: null,
          departmentFullName: null,
          departmentFullCode: null,
          contractNumber: null,
          major: null,
          foundSource: null,
          budget: null,
          auditTime: null,
          projectId: null,
          projectNo: null,
          projectName: null,
          projectStatus: null,
          status: null
        }
      }
    },
    methods: {
      create,
      load,
      save,
      update,
      setProject({ id, projectName, projectNo }) {
        this.entity.projectId = id
        this.entity.projectName = projectName
        this.entity.projectNo = projectNo
      },
      setDepartment([{ fullcode, fullname }]) {
        this.entity.departmentFullName = fullname
        this.entity.departmentFullCode = fullcode
      }
    }
  }
</script>
